from rest_framework.views import APIView
from apps.users.models import User
from rest_framework.response import Response
from datetime import date, timedelta


class UserDailyActiveCountView(APIView):
    """日活用户"""
    def get(self, request):
        today = date.today()  # yyyy-mm-dd
        # count = User.objects.filter(last_login__gte=today).count()
        count = User.objects.filter(last_login__gte=today).count()
        return Response({'count': count})


class UserMonthCountView(APIView):
    """月活用户"""
    def get(self, request):
        today = date.today()
        start = today - timedelta(days=30)
        date_list = []
        for i in range(30):
            index_date = start + timedelta(days=i)
            next_date = start + timedelta(days=i + 1)
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=next_date).count()
            date_list.append({
                'count': count,
                'date': index_date
            })
        return Response(date_list)


